﻿var companyId = '';
var ztId = '';
var kjnd = '';
var kjkm = [];
//var aj = [];
//var pjh = [];
//var zph = [];
//var wldx = [];
//var xm = [];
//var bm = [];

var userdefault = {};
var params = {};
var dataList = [];
var mapList = [{id: "col_kjkmCode", field: "kjkmCode"}, {id: "col_kjkmName", field: "kjkmName"},
{id: "col_yefx1Str", field: "yefx1Str"},  {id: "col_ncye", field: "ncye"},
{id: "col_yefx2Str", field: "yefx2Str"}, {id: "col_qcye", field: "qcye" },
{id: "col_jfs", field: "jfs"}, {id: "col_dfs", field: "dfs"}, {id: "col_jlj", field: "jlj"},
{id: "col_dlj", field: "dlj"}, {id: "col_yefx3Str",  field: "yefx3Str" }, {id: "col_ye", field: "ye"}];

//创建一个表格控件
var tbl = new QTable();

var bootstrap = function ($, learun) {

    var pageWidth = 1024;

    "use strict";
    var page = {
        init: function () {
            userdefault = learun.clientdata.get(['usersetting']);

            //初始化打印插件
            hiprint.init();
            pageWidth = ($('.lr-layout-grid').width());
            $("#divDataList").height($(window).height() - 160);

            tbl.BindDom("divDataList"); //绑定到模板
            tbl.setMaplist(mapList);  //设置数据映射关系
            page.bind();


        },
        bind: function () {
            $('#multiple_condition_query').lrMultipleQuery(function (queryJson) {
                page.search(queryJson);
            }, 400, 800);
            $('#pzrqMonth').lrselect({
                type: 'default',
                // 展开最大高度
                maxHeight: 300,
                placeholder: '月份',
                data: [
                    {'id': '01', 'text': '01'},
                    {'id': '02', 'text': '02'},
                    {'id': '03', 'text': '03'},
                    {'id': '04', 'text': '04'},
                    {'id': '05', 'text': '05'},
                    {'id': '06', 'text': '06'},
                    {'id': '07', 'text': '07'},
                    {'id': '08', 'text': '08'},
                    {'id': '09', 'text': '09'},
                    {'id': '10', 'text': '10'},
                    {'id': '11', 'text': '11'},
                    {'id': '12', 'text': '12'}
                ],
                select: function (item) {
                    if (!item || item.id == '-1') {
                        var beginDate = kjnd + "-01-01";
                        var day = new Date(kjnd, 12, 0);
                        var endDate = kjnd + "-" + 12 + "-" + day.getDate();
                        $("#pzrqStart").val(beginDate);
                        $("#pzrqEnd").val(endDate);
                    } else {
                        var kjqj = item.id;
                        var beginDate = kjnd + "-" + kjqj + "-01";
                        var day = new Date(kjnd, kjqj, 0);
                        var endDate = kjnd + "-" + kjqj + "-" + day.getDate();
                        $("#pzrqStart").val(beginDate);
                        $("#pzrqEnd").val(endDate);
                    }
                }
            });
            // 公司选择
            $('#company_select').lrselect({
                type: 'tree',
                // placeholder: '请选择机构',
                height: 300,
                maxHeight: 300,
                // 是否允许搜索
                //allowSearch: true,
                select: function (item) {
                    if (item && item.value != '-1') {
                        companyId = item.value;
                    }
                }
            });
            $('#company_select').lrselectRefresh({
                // 访问数据接口地址
                url: config.base_server() + 'companys/tree?access_token=' + config.getToken().access_token,
                // 访问数据接口参数
                param: {parentId: '0'}
            }).lrselectSet(userdefault.companyId);

            // 帐套选择
            $('#zt_select').lrDataItemSelect({
                code: 'ZT', maxHeight: 100,
                select: function (item) {
                    if (!item || item.id == '-1') {
                        ztId = '';
                    } else {
                        ztId = item.id;
                        $("#zt_title").html(item.text);
                    }
                }
            }).lrselectSetFirst();

            // 凭证类型
            $('#pzlxCode').lrDataItemSelect({
                code: 'PZLXS', maxHeight: 100,placeholder: "==请选择凭证类型==",
                select: function (item) {

                }
            });

            // 会计年度选择
            $('#kjnd_select').lrDataItemSelect({
                code: 'YEAR', maxHeight: 100, placeholder: "==请选择年份==",
                select: function (item) {
                    if (!item || item.value == '-1') {
                        kjnd = '';
                    } else {
                        kjnd = item.value;

                        var beginDate = kjnd + "-01-01";
                        var day = new Date(kjnd, 12, 0);
                        var endDate = kjnd + "-12-" + day.getDate();
                        $("#pzrqStart").val(beginDate);
                        $("#pzrqEnd").val(endDate);

                        //凭证类型
                        $("#pzlxCode").lrselectRefresh({
                            url: config.base_server() + 'pzlxs/list?access_token=' + config.getToken().access_token,
                            param: {companyId: companyId, ztId: ztId, kjnd: kjnd, status: 1}
                        });

                        $('#kjkmCodes').lrlayerselect({
                            dataValueId: 'chrCode',
                            dataTextId: 'codeAndName',
                            reset: true,
                            url: config.base_server() + 'kjkms/list?companyId=' + companyId + '&ztId=' + ztId + '&kjnd=' + kjnd + '&status=1' + '&access_token=' + config.getToken().access_token,
                            grid: [
                                {label: '科目', name: 'codeAndName', width: 225, align: 'left'},
                                {
                                    label: "余额方向", name: "yefx", width: 50, align: "center",
                                    formatter: function (cellvalue) {
                                        return cellvalue == 1 ? "借" : "贷";
                                    }
                                },
                                {label: '是否明细', name: 'isLeaf', width: 1, align: 'left', hidden: true},
                                {label: '编号', name: 'chrCode', width: 120, align: 'left', hidden: true},
                                {label: '简称', name: 'chrName', width: 225, align: 'left', hidden: true}
                            ],
                            select: function (values, texts) {
                                kjkm = values;
                            }
                        });
//                        $('#xmCodes').lrlayerselect({
//                            dataValueId: 'code',
//                            dataTextId: 'codeAndName',
//                            reset: true,
//                            url: config.base_server() + 'xms/list?companyId=' + companyId + '&ztId=' + ztId + '&kjnd=' + kjnd + '&status=1' + '&access_token=' + config.getToken().access_token,
//                            grid: [
//                                {label: '名称', name: 'codeAndName', width: 225, align: 'left'},
//                                {label: '备注', name: 'remark', width: 225, align: 'left'},
//                                {label: '编号', name: 'code', width: 120, align: 'left'},
//                            ],
//                            select: function (values, texts) {
//                                xm = values;
//                            }
//                        });
//                        $('#bmCodes').lrlayerselect({
//                            dataValueId: 'code',
//                            dataTextId: 'codeAndName',
//                            reset: true,
//                            url: config.base_server() + 'departments?companyId=' + companyId + '&ztId=' + ztId + '&kjnd=' + kjnd + '&status=1' + '&access_token=' + config.getToken().access_token,
//                            grid: [
//                                {label: '名称', name: 'codeAndName', width: 225, align: 'left'},
//                                {label: '备注', name: 'remark', width: 225, align: 'left'},
//                                {label: '编号', name: 'code', width: 120, align: 'left'},
//                            ],
//                            select: function (values, texts) {
//                                bm = values;
//                            }
//                        });
//                        $('#ajCodes').lrlayerselect({
//                            dataValueId: 'name',
//                            dataTextId: 'name',
//                            enableInput: true,
//                            reset: true,
//                            url: config.base_server() + 'ajs/list?companyId=' + companyId + '&ztId=' + ztId + '&kjnd=' + kjnd + '&status=1' + '&access_token=' + config.getToken().access_token,
//                            grid: [
//                                {label: '名称', name: 'name', width: 225, align: 'left'},
//                                {label: '备注', name: 'remark', width: 225, align: 'left'},
//                            ],
//                            select: function (values, texts) {
//                                aj = values;
//                            }
//                        });
//                        $('#pjhCodes').lrlayerselect({
//                            dataValueId: 'name',
//                            dataTextId: 'name',
//                            enableInput: true,
//                            reset: true,
//                            url: config.base_server() + 'pjhs/list?companyId=' + companyId + '&ztId=' + ztId + '&kjnd=' + kjnd + '&type=1' + '&status=1' + '&access_token=' + config.getToken().access_token,
//                            grid: [
//                                {label: '名称', name: 'name', width: 225, align: 'left'},
//                                {label: '备注', name: 'remark', width: 225, align: 'left'},
//                            ],
//                            select: function (values, texts) {
//                                pjh = values;
//                            }
//                        });
//                        $('#zphCodes').lrlayerselect({
//                            dataValueId: 'name',
//                            dataTextId: 'name',
//                            enableInput: true,
//                            reset: true,
//                            url: config.base_server() + 'pjhs/list?companyId=' + companyId + '&ztId=' + ztId + '&kjnd=' + kjnd + '&type=2' + '&status=1' + '&access_token=' + config.getToken().access_token,
//                            grid: [
//                                {label: '名称', name: 'name', width: 225, align: 'left'},
//                                {label: '备注', name: 'remark', width: 225, align: 'left'},
//                            ],
//                            select: function (values, texts) {
//                                pjh = values;
//                            }
//                        });
//                        $('#wldxCodes').lrlayerselect({
//                            dataValueId: 'code',
//                            dataTextId: 'codeAndName',
//                            enableInput: true,
//                            reset: true,
//                            url: config.base_server() + 'wldxs/list?companyId=' + companyId + '&ztId=' + ztId + '&kjnd=' + kjnd + '&status=1' + '&access_token=' + config.getToken().access_token,
//                            grid: [
//                                {label: '名称', name: 'codeAndName', width: 225, align: 'left'},
//                                {label: '备注', name: 'remark', width: 225, align: 'left'},
//                                {label: '编号', name: 'code', width: 120, align: 'left', hidden: true},
//                            ],
//                            select: function (values, texts) {
//                                wldx = values;
//                            }
//                        });

                        page.search({});
                    }
                }
            });

            // 查询
            $('#btn_Search').on('click', function () {
                page.search();
            });

            // 刷新
            $('#lr_refresh').on('click', function () {
                location.reload();
            });

            //打印
            $('#lr_print').on('click', function () {
                var hiprintTemplate = null;
                learun.httpAsyncGet2(config.base_server() + 'printTempletes/findByCode', "&companyId=" + companyId + '&ztId=' + ztId + '&code=kmyeb', function (res) {
                    if (res.resp_code == 200) {
                        hiprintTemplate = new hiprint.PrintTemplate({
                            template: JSON.parse(res.data.templete),
                            paginationContainer: '.hiprint-printPagination'
                        });

                        //hiprintTemplate.design('#hiprint-printTemplate');

                        //var rowdatas = $('#gridtable').jfGridGet('rowdatas');
                        var item = {};
                        item.pzflList = dataList;
                        // for (var i = 0; i < item.pzflList.length; i++) {
                        //     item.pzflList[i].ncye = $.formatMoney(item.pzflList[i].ncye, 2);
                        //     item.pzflList[i].qcye = $.formatMoney(item.pzflList[i].qcye, 2);
                        //     item.pzflList[i].jfs = $.formatMoney(item.pzflList[i].jfs, 2);
                        //     item.pzflList[i].dfs = $.formatMoney(item.pzflList[i].dfs, 2);
                        //     item.pzflList[i].jlj = $.formatMoney(item.pzflList[i].jlj, 2);
                        //     item.pzflList[i].dlj = $.formatMoney(item.pzflList[i].dlj, 2);
                        //     item.pzflList[i].ye = $.formatMoney(item.pzflList[i].ye, 2);
                        // }
                        item.ztName = $("#zt_select .lr-select-placeholder").html();
                        var pzrqStart = $("#pzrqStart").val();
                        var pzrqEnd = $("#pzrqEnd").val();
                        item.pzrq = pzrqStart.substring(0, 10) + "至" + pzrqEnd.substring(0, 10);
                        // hiprintTemplate.print(item);
                        hiprintTemplate.resetPage();
                        $('#myModal .modal-body .prevViewDiv').html(hiprintTemplate.getHtml(item));
                        $('#myModal').modal('show');
                    }
                });
                $('#A4_printByHtml').click(function () {
                    hiprintTemplate.printByHtml($('#myModal .modal-body .prevViewDiv'));
                })
            });

            //导出Excel
            $("#lr_export_excel").on('click', function () {
                if (params.companyId == undefined) {
                    learun.alert.warning('请先查询要导出的数据！');
                    return;
                }
                params.access_token = config.getToken().access_token;

                learun.download({
                    url: config.base_server() + 'reports/kmyeb/excel',
                    param: params
                });
            });
            //导出pdf
            $("#lr_export_pdf").on('click', function () {
                if (params.companyId == undefined) {
                    learun.alert.warning('请先查询要导出的数据！');
                    return;
                }
                params.access_token = config.getToken().access_token;

                learun.download({
                    url: config.base_server() + 'reports/kmyeb/pdf',
                    param: params
                });
            });
        },

        // initGrid: function () {
        //     $("#gridtable").height($(window).height() - 170);
        // },

        search: function (param) {
            param = param || {};
            var pzrqStart = $("#pzrqStart").val();
            var pzrqEnd = $("#pzrqEnd").val();
            param.companyId = companyId;
            param.ztId = ztId;
            param.kjnd = kjnd;
            param.pzrqStart = pzrqStart;
            param.pzrqEnd = pzrqEnd;
            if ($.isArray(kjkm)) {
                param.kjkmCodes = kjkm.join();
            }

            if (!param.isWithoutZero) {
                param.isWithoutZero = 1;
            }
            params = param;
            learun.loading(true);
            learun.httpPost2(config.base_server() + 'reports/kmyeb?access_token=' + config.getToken().access_token, param, function (res) {
                learun.loading(false);
                if (res.resp_code == 200) {
                    dataList = res.data;
                    for (var i = 0; i < dataList.length; i++) {
                        dataList[i].ncye = $.formatMoney(dataList[i].ncye, 2);
                        dataList[i].qcye = $.formatMoney(dataList[i].qcye, 2);
                        dataList[i].jfs = $.formatMoney(dataList[i].jfs, 2);
                        dataList[i].dfs = $.formatMoney(dataList[i].dfs, 2);
                        dataList[i].jlj = $.formatMoney(dataList[i].jlj, 2);
                        dataList[i].dlj = $.formatMoney(dataList[i].dlj, 2);
                        dataList[i].ye = $.formatMoney(dataList[i].ye, 2);
                    }
                    tbl.BindData(dataList);
                } else {
                    learun.alert.error(res.resp_msg);
                    learun.httpErrorLog(res.resp_msg);
                }
            });

            $("#pzrq_title").html(pzrqStart.substring(0, 10) + "至" + pzrqEnd.substring(0, 10));
        }
    };
    page.init();
}

//查看明细账
function ViewData(row) {
    var data = row.data;
    var kjkm = data.kjkmCode;
    var ajs = $("#ajCodes input").val().split(",");
    var ajArr = [];
    var aj;
    for (var i = 0; i < ajs.length; i++) {
        if (ajs[i].length > 1) {
            ajArr.push(ajs[i]);
        }
    }
    aj = ajArr.join();
    var pjhs = $("#pjhCodes input").val().split(",");
    var pjhArr = [];
    var pjh;
    for (var i = 0; i < pjhs.length; i++) {
        if (pjhs[i].length > 1) {
            pjhArr.push(pjhs[i]);
        }
    }
    pjh = pjhArr.join();
    var zphs = $("#zphCodes input").val().split(",");
    var zphArr = [];
    var zph;
    for (var i = 0; i < zphs.length; i++) {
        if (zphs[i].length > 1) {
            zphArr.push(zphs[i]);
        }
    }
    zph = zphArr.join();
    var wldxs = $("#wldxCodes input").val().split(",");
    var wldxArr = [];
    var wldx;
    for (var i = 0; i < wldxs.length; i++) {
        if (wldxs[i].split(" ").length > 1) {
            wldxArr.push(wldxs[i].split(" ")[0]);
        }
    }
    wldx = wldxArr.join();
    var xms = $("#xmCodes span").html().split(",");
    var xmArr = [];
    var xm;
    for (var i = 0; i < xms.length; i++) {
        if (xms[i].split(" ").length > 1) {
            xmArr.push(xms[i].split(" ")[0]);
        }
    }
    xm = xmArr.join();
    var bms = $("#bmCodes span").html().split(",");
    var bmArr = [];
    var bm;
    for (var i = 0; i < bms.length; i++) {
        if (bms[i].split(" ").length > 1) {
            bmArr.push(bms[i].split(" ")[0]);
        }
    }
    bm = bmArr.join();
    var pzrqStart = $("#pzrqStart").val();
    var pzrqEnd = $("#pzrqEnd").val();
    var kjkmName = data.kjkmCode + ' ' + data.kjkmName;
    top.learun.frameTab.open({
        id: 'kmmxz_view',
        css: 'fa fa-file-text-o',
        name: '科目三栏账',
        path: config.webportal_server + 'pages/finance/report/slmxzReport2.html?companyId=' + companyId + '&ztId=' + ztId + '&kjnd=' + kjnd + '&kjkm=' + kjkm + '&kjkmName=' + encodeURI(encodeURI(kjkmName)) + '&aj=' + encodeURI(encodeURI(aj)) + '&pjh=' + encodeURI(encodeURI(pjh)) + '&zph=' + encodeURI(encodeURI(zph)) + '&wldx=' + encodeURI(encodeURI(wldx)) + '&xm=' + encodeURI(encodeURI(xm)) + '&bm=' + encodeURI(encodeURI(bm)) + '&pzrqStart=' + pzrqStart + '&pzrqEnd=' + pzrqEnd + '&access_token=' + config.getToken().access_token
    });
}
